﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace UIT_CV_Management
{
    public class QuyTrinhDaoTao
    {
        public String maQuaTrinhDaoTao;
        String maDaoTaoDaiHoc;
        String maDaoTaoTrenDaiHoc;
        String maDaoTaoNgoaiNgu;
        String maDaoTaoHocHam;

        //Database
        Database myDatabase = Database.shareDatabase;

        //object
        public List<DaoTaoDaiHoc> listDaoTaoDaiHoc
        {
            get
            {
                QueryResult result = myDatabase.Query("SELECT * FROM QuaTrinhDaoTao WHERE MaDaoTaoDaiHoc = '" + maDaoTaoDaiHoc + "'");
                if (result.ReturnCode == 200)
                {
                    listDaoTaoDaiHoc = new List<DaoTaoDaiHoc>();

                    for (int i = 0; i < result.Table.Rows.Count; i++)
                    {
                        DataRow row = result.Table.Rows[i];
                        DaoTaoDaiHoc daiHoc = new DaoTaoDaiHoc(row["MaDaoTaoDaiHoc"].ToString());
                        listDaoTaoDaiHoc.Add(daiHoc);
                    }
                }
                return listDaoTaoDaiHoc;
            }
            set
            {
                listDaoTaoDaiHoc = value;
            }
        }
        List<DaoTaoTrenDaiHoc> listDaoTaoTrenDaiHoc
        {
            get
            {
                QueryResult result = myDatabase.Query("SELECT * FROM QuaTrinhDaoTao WHERE MaDaoTaoTrenDaiHoc = '" + maDaoTaoTrenDaiHoc + "'");
                if (result.ReturnCode == 200)
                {
                    listDaoTaoTrenDaiHoc = new List<DaoTaoTrenDaiHoc>();

                    for (int i = 0; i < result.Table.Rows.Count; i++)
                    {
                        DataRow row = result.Table.Rows[i];
                        DaoTaoTrenDaiHoc trenDaiHoc = new DaoTaoTrenDaiHoc(row["MaDaoTaoTrenDaiHoc"].ToString());
                        listDaoTaoTrenDaiHoc.Add(trenDaiHoc);
                    }
                }
                return listDaoTaoTrenDaiHoc;
            }
            set
            {
                listDaoTaoTrenDaiHoc = value;
            }
        }
        List<DaoTaoNgoaiNgu> listDaoTaoNgoaiNgu
        {
            get
            {
                return DaoTaoNgoaiNgu.getAllDaoTaoNgoaiNgu(maDaoTaoNgoaiNgu);
            }
            set
            {
                listDaoTaoNgoaiNgu = value;
            }
        }
        List<DaoTaoHocHam> listDaoTaoHocHam
        {
            get
            {
                return DaoTaoHocHam.getAllDaoTaoHocHam(maDaoTaoHocHam);
            }
            set
            {
                listDaoTaoHocHam = value;
            }
        }

        public QuyTrinhDaoTao(String _maDaoTaoDaiHoc, String _maDaoTaoTrenDaiHoc, String _maDaoTaoNgoaiNgu, String _maDaoTaoHocHam)
        {
            maQuaTrinhDaoTao = Database.getID("QuaTrinhDaoTao", "MaQuaTrinhDaoTao");
            maDaoTaoDaiHoc = _maDaoTaoDaiHoc;
            maDaoTaoTrenDaiHoc = _maDaoTaoTrenDaiHoc;
            maDaoTaoNgoaiNgu = _maDaoTaoNgoaiNgu;
            maDaoTaoHocHam = _maDaoTaoHocHam;
        }
        public QuyTrinhDaoTao(String _maQuaTrinhDaoTao)
        {
            QueryResult result = myDatabase.Query("SELECT * FROM QuaTrinhDaoTao WHERE MaQuaTrinhDaoTao = '"
                + _maQuaTrinhDaoTao + "'");
            if (result.ReturnCode == 200)
            {
                maQuaTrinhDaoTao = result.Table.Rows[0]["MaQuaTrinhDaoTao"].ToString();
                maDaoTaoDaiHoc = result.Table.Rows[0]["MaDaoTaoDaiHoc"].ToString();
                maDaoTaoTrenDaiHoc = result.Table.Rows[0]["MaDaoTaoTrenDaiHoc"].ToString();
                maDaoTaoNgoaiNgu = result.Table.Rows[0]["MaDaoTaoNgoaiNgu"].ToString();
                maDaoTaoHocHam = result.Table.Rows[0]["MaDaoTaoHocHam"].ToString();

            }
        }
        public Boolean save()
        {
            QueryResult result;
            //Insert
            result = Database.shareDatabase.Query("INSERT INTO QuaTrinhDaoTao VALUES(" +
                "'" + maQuaTrinhDaoTao + "', " +
                "'" + maDaoTaoDaiHoc + "', " +
                "'" + maDaoTaoTrenDaiHoc + "', " +
                "'" + maDaoTaoNgoaiNgu + "', " +
                "'" + maDaoTaoHocHam + "')");
            if (result.ReturnCode == 200)
                return true;
            else
                return false;
        }
        public Boolean delete()
        {
            QueryResult result = Database.shareDatabase.Query("DELETE FROM QuaTrinhDaoTao" +
                "WHERE MaQuaTrinhDaoTao = '" + maQuaTrinhDaoTao + "'");
            if (result.ReturnCode == 200)
            {
                // delete All DaoTaoDaiHoc
                foreach (DaoTaoDaiHoc daiHoc in listDaoTaoDaiHoc)
                    daiHoc.delete();

                // delete All DaoTaoTrenDaiHoc
                foreach (DaoTaoTrenDaiHoc trenDaiHoc in listDaoTaoTrenDaiHoc)
                    trenDaiHoc.delete();

                // delete All DaoTaoNgoaiNgu
                foreach (DaoTaoNgoaiNgu ngoaiNgu in listDaoTaoNgoaiNgu)
                    ngoaiNgu.Delete();

                // delete all HocHam
                foreach (DaoTaoHocHam hocHam in listDaoTaoHocHam)
                    hocHam.Delete();
                return true;
            }
            else
                return false;
        }
    }
}
